package com.bc004346.travelus;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.bc004346.travelus.Fragment.FlightDetailsFragment;
import com.bc004346.travelus.Model.Domain.Flight;

import java.util.ArrayList;

import static com.bc004346.travelus.Fragment.FlightDetailsFragment.EXTRA_FLIGHTS;
import static com.bc004346.travelus.Fragment.FlightDetailsFragment.EXTRA_FLIGHT_ID;

/**
 * Created by Renats on 9/27/2014.
 * Wrapper for flight details to allow swiping
 */
public class FlightPagerActivity extends FragmentActivity {

    private ArrayList<Flight> mFlights;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewPager mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.viewPager);
        setContentView(mViewPager);

        //noinspection unchecked
        mFlights = (ArrayList<Flight>) getIntent()
                .getSerializableExtra(EXTRA_FLIGHTS);
        int id = getIntent().getIntExtra(EXTRA_FLIGHT_ID, 0);

        mViewPager.setOffscreenPageLimit(0);

        FragmentManager fm = getSupportFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
            @Override
            public Fragment getItem(int i) {
                Flight flight = mFlights.get(i);
                return FlightDetailsFragment.newInstance(flight);
            }

            @Override
            public int getCount() {
                return mFlights.size();
            }
        });

        mViewPager.setCurrentItem(id);
    }
}
